#region

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using InfoTech.Core.Repository;
using VinhSon.Intranet.Bussiness;
using VinhSon.Intranet.IRepository;

#endregion

namespace VinhSon.Intranet.Repository
{
    public partial class PermissionRepository : BaseEntityRepository<VinhSonContext, Permission>,
                                                IPermissionRepository
    {
        #region Constructor 

        public PermissionRepository(VinhSonContext context)
        {
            DataContext = context;
        }

        #endregion

        #region Implementation of IBaseRepository<VinhSonDataContext>

        public virtual void Delete(Int32 value)
        {
            Permission temp = SelectbyId(value);
            Delete(temp);
        }

        public virtual void Delete(Int32 value,
                                   out Exception exception)
        {
            try
            {
                Delete(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual void Delete(IEnumerable<Int32> value)
        {
            IQueryable<Permission> query = DbSet.Where(c => value.Contains(c.Id));
            Delete(query);
        }

        public virtual void Delete(IEnumerable<Int32> value,
                                   out Exception exception)
        {
            try
            {
                Delete(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual void Delete(Permission value)
        {
            //if (DataContext.Entry(value).State == EntityState.Detached)
            //    DbSet.Attach(value);
            //DataContext.Entry(value).State = EntityState.Deleted;

            value.Inactive = true;
            value.LastModified = DateTime.Now;

            if(DataContext.Entry(value)
                          .State == EntityState.Detached)
                DbSet.Attach(value);
            DataContext.Entry(value)
                       .State = EntityState.Modified;
        }

        public virtual void Delete(Permission value,
                                   out Exception exception)
        {
            try
            {
                Delete(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual void Delete(IEnumerable<Permission> value)
        {
            // SetEntityState(value, EntityState.Deleted);
            foreach (Permission item in value)
            {
                Delete(item);
            }
        }

        public virtual void Delete(IEnumerable<Permission> value,
                                   out Exception exception)
        {
            try
            {
                Delete(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual void Insert(Permission value)
        {
            value.Inactive = false;
            value.LastModified = DateTime.Now;

            DbSet.Add(value);
        }

        public virtual void Insert(Permission value,
                                   out Exception exception)
        {
            try
            {
                Insert(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        public virtual IQueryable<Permission> Select()
        {
            return DbSet;
        }

        public virtual IQueryable<Permission> Select(out Exception exception)
        {
            try
            {
                IQueryable<Permission> query = Select();
                exception = null;
                return query;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return null;
        }

        public virtual IQueryable<Permission> Select(Expression<Func<Permission, bool>> predicate,
                                                     string include = "")
        {
            IQueryable<Permission> query = DbSet;
            if(predicate != null)
                query = query.Where(predicate);
            if(!string.IsNullOrEmpty(include))
            {
                string[] s = include.Split(',');
                query = s.Aggregate(query,
                                    (current,
                                     s1) => current.Include(s1));
            }
            return query;
        }

        public virtual IQueryable<Permission> Select(Expression<Func<Permission, bool>> predicate,
                                                     string include,
                                                     out Exception exception)
        {
            try
            {
                IQueryable<Permission> query = Select(predicate,
                                                      include);
                exception = null;
                return query;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return null;
        }

        public virtual IQueryable<Permission> Select<TKey>(Expression<Func<Permission, bool>> predicate = null,
                                                           Expression<Func<Permission, TKey>> keySelector = null,
                                                           ListSortDirection direction = ListSortDirection.Ascending,
                                                           string include = "")
        {
            IQueryable<Permission> query = DbSet;
            if(predicate != null)
                query = query.Where(predicate);
            if(keySelector != null)
                query = direction == ListSortDirection.Descending
                                ? query.OrderByDescending(keySelector)
                                : query.OrderBy(keySelector);
            if(!string.IsNullOrEmpty(include))
            {
                string[] s = include.Split(',');
                query = s.Aggregate(query,
                                    (current,
                                     s1) => current.Include(s1));
            }
            return query;
        }

        public virtual IQueryable<Permission> Select<TKey>(Expression<Func<Permission, bool>> predicate,
                                                           Expression<Func<Permission, TKey>> keySelector,
                                                           ListSortDirection direction,
                                                           string include,
                                                           out Exception exception)
        {
            try
            {
                IQueryable<Permission> query = Select(predicate,
                                                      keySelector,
                                                      direction,
                                                      include);
                exception = null;
                return query;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return null;
        }

        public virtual Permission SelectbyId(Int32 value)
        {
            Permission temp = DbSet.Find(value);
            return temp;
        }

        public virtual Permission SelectbyId(Int32 value,
                                             out Exception exception)
        {
            try
            {
                Permission temp = SelectbyId(value);
                exception = null;
                return temp;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return null;
        }

        public virtual void Update(Permission value)
        {
            value.LastModified = DateTime.Now;

            if(DataContext.Entry(value)
                          .State == EntityState.Detached)
                DbSet.Attach(value);
            DataContext.Entry(value)
                       .State = EntityState.Modified;
        }

        public virtual void Update(Permission value,
                                   out Exception exception)
        {
            try
            {
                Update(value);
                exception = null;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }

        #endregion
    }
}
